const { baseURL } = require("./base");
const payUrl="/app-api/pay/order/submit"
const pay = function(data,type="wx_lite"){
	
	return new Promise((resolve, reject) => {
		// var url=type=="payvip" ? "/api/vip/pay" : "/api/paper/pay"
		
		uni.request({
		   url:`${baseURL}${payUrl}`,
			data,	
			header:{
				Authorization:"Bearer "+ uni.getStorageSync('token'),
				"tenant-id":"1"
			}, 			
			method :"POST",
			success: (res) => {
				if(res.data.code==0 ){		
					if(type=="wx_lite"){
						let wxpaly=JSON.parse(res.data.data.displayContent)
						console.log(wxpaly)
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: wxpaly.timeStamp,
							nonceStr: wxpaly.nonceStr,
							package: wxpaly.packageValue,
							signType: wxpaly.signType,
							paySign: wxpaly.paySign,						
							success: function (rest) {
								console.log('支付success:' , rest);
								resolve(wxpaly); 
							},
							fail: function (err) {
						        
								reject(err);
								uni.showToast({
								    title: "支付失败",
								    duration: 2000,
									icon:'none'
								}); 
							}
						})
					}else{
						if(res.data.data.status==10){
							resolve(res); 
						}else{
							reject(res);
						}
						
					}
					
					
				}else{
					reject(res.data);
                    //接口报错
					// uni.showToast({
					//     title: res.data.msg,
					//     duration: 2000,
					// 	icon:'none'
					// });
				}
				
				
			}
		});
		
	});
	 
}
module.exports = {
   pay
}